<?php

namespace IDock\Insurance\TPBX;

use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\TPBX\Request\EPolicyDownloadRequest;
use IDock\Insurance\TPBX\Request\NonCarProductDetailRequest;
use IDock\Insurance\TPBX\Request\PolicyPaymentRequest;
use IDock\Insurance\TPBX\Request\PremiumInquiryRequest;
use IDock\Insurance\TPBX\Request\ProposalStatusRequest;
use IDock\Insurance\TPBX\Request\RenewalInquiryRequest;
use IDock\Insurance\TPBX\Request\UnderwritingRequest;
use IDock\Insurance\TPBX\Request\UploadFileRequest;
use IDock\Insurance\TPBX\Request\ValidateCarRequest;
use IDock\Insurance\TPBX\Request\VehicleModelRequest;
use IDock\Insurance\TPBX\Response\EPolicyDownloadResponse;
use IDock\Insurance\TPBX\Response\NonCarProductDetailResponse;
use IDock\Insurance\TPBX\Response\NonCarProductListResponse;
use IDock\Insurance\TPBX\Response\PolicyPaymentResponse;
use IDock\Insurance\TPBX\Response\PremiumInquiryResponse;
use IDock\Insurance\TPBX\Response\ProposalStatusResponse;
use IDock\Insurance\TPBX\Response\RenewalInquiryResponse;
use IDock\Insurance\TPBX\Response\UnderwritingResponse;
use IDock\Insurance\TPBX\Response\UploadFileResponse;
use IDock\Insurance\TPBX\Response\ValidateCarResponse;
use IDock\Insurance\TPBX\Response\VehicleActualValueResponse;
use IDock\Insurance\TPBX\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarUnderwritingPrinter;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 珠峰保险
 */
class TPBXConstruct extends Builder implements IDockInterface
{

    protected $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->channelName = $config['channelName'] ?? '';
        $this->appId = $config['appId'] ?? '';
        $this->appKey = $config['appKey'] ?? '';
        $this->notifyUrl = $config['notifyUrl'] ?? '';
        $this->fileGateway = $config['fileGateway'] ?? '';
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     * @version 4.0
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);
        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格接口
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): Response
    {

        //调用ACIC
        $response = new \IDock\Insurance\ACIC\Response\VehicleActualValueResponse();
        $vehicleActualValueVisitor->vehiclePrice = $vehicleActualValueVisitor->vehiclePriceWithOutTax;
        $response->setCode(200);
        $response->setVisitor($vehicleActualValueVisitor);
        $response->printerInit();
        return $response;

        //$response = new VehicleActualValueResponse();
        //$response->setCode('200');
        //$vehicleActualValuePrinter = new VehicleActualValuePrinter();
        //$response->printer = $vehicleActualValuePrinter;
        //return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PremiumInquiryResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): PremiumInquiryResponse
    {
        $request = new PremiumInquiryRequest();
        $request->setVisitor($premiumInquiryVisitor);

//global $testResponse;
//        $testResponse = '{"msg":"业务执行异常:[车险核心]报价错误：交强险重复投保，警告：号牌号码“川A1Q5Z0”的保单发生重复投保，与其重复投保的其它公司的保单信息如下：投保确认码 02PAIC510023080271045167009934;保单号 12611893902129927930;起保日期 2023-08-18 00:00;终保日期 2024-08-18 00:00;号牌号码 川A35FV1;号牌种类 02;车架号 LVYPDALA7JP018090;发动机号 7035037;地区 四川。","code":"1000028"}';

        /**@var PremiumInquiryResponse $response * */
        $response = $this->execute($request);
        if ($response->printer->repeatInsuredFlg && $premiumInquiryVisitor->biStartData) {
            $response->printer->repeatInsurePrinter['biStartData'] = $response->printer->repeatInsurePrinter['ciStartData'];
        }
        if ($response->isSuccess()) {
            $response->buildCoverage($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy, $premiumInquiryVisitor->getCoverageVisitorList());
        }
        $nonCarInquiryPrinter = new NonCarInquiryPrinter();
        $totalPremium = 0;
        foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
            /*** @var $item NonCarProductVisitor */
            $single = new NonCarInquirySinglePrinter();
            $single->premium = $item->premium;
            $single->planCode = $item->planCode;
            $single->productCode = $item->productCode;
            $single->statusCode = 'success';
            $totalPremium += $single->premium * $item->insuredNum;
            $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
        }
        $nonCarInquiryPrinter->totalPremium = $totalPremium;
        $response->printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return UnderwritingResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): UnderwritingResponse
    {
        $attachmentList = [];
        if (!empty($premiumInquiryVisitor->getFileList())) {
            foreach ($premiumInquiryVisitor->getFileList() as $value) {
                $request1 = new UploadFileRequest();
                $uploadFileVisitor = new UploadFileVisitor();
                $uploadFileVisitor->argc = $premiumInquiryVisitor->getExtInfoVisitor()->argc;
                $uploadFileVisitor->fileAppend($value['type'], $value['url']);
                $request1->setVisitor($uploadFileVisitor);
                $response1 = $this->execute($request1);
                if ($response1->isSuccess()) {
                    $value['number'] = $response1->number;
                    $attachmentList[] = $value;
                }
            }
        }
        $request = new UnderwritingRequest();
        $request->attachmentList = $attachmentList;
        $request->setVisitor($premiumInquiryVisitor);
//        global $testResponse;
//        $testResponse = '{    "code": "0000",    "msg": "成功",    "data": {        "head": {            "requestType": "V050",            "account": "15520756896_guangdong",            "password": "666666"        },        "basePart": {            "tpTPFApplyNo": "1000105000016043085",            "tpBizApplyNo": "1000109000016043086",            "accidentApplyNo": "",    "healthApplyNo": "",            "attachRulesString": ""        }    }}';
        /**@var UnderwritingResponse $response * */
        $response = $this->execute($request);
        if (!empty($response->printer->argc['nonCarInquiryNo'])) {
            /***
             * @var NonCarInquirySinglePrinter $nonCarSingle
             */
            $nonCarSingle = $premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList()[0] ?? [];
            if ($nonCarSingle) {
                $single = new NonCarInquirySinglePrinter();
                $single->planCode = $nonCarSingle->planCode;
                $single->productCode = $nonCarSingle->productCode;
                $single->inquiryNo = $response->printer->argc['nonCarInquiryNo'] ?? '';
                $nonCarInquiryPrinter = new NonCarUnderwritingPrinter();
                $nonCarInquiryPrinter->nonCarSingleAppend($single);
                $response->printer->noCarUnderwritingPrinter = $nonCarInquiryPrinter;
            }
        }
        return $response;
    }

    /**
     * 获取支付链接
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);

        /**@var PolicyPaymentResponse $response * */
        $response = $this->execute($request);

        return $response;
    }

    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): ProposalStatusResponse
    {
        $request = new ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);

        /**
         * @var ProposalStatusResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function proposalRecall(ProposalRecallVisitor $revokeVisitor)
    {
    }

    /**
     * 撤单接口
     * @param UnderwritingRecallVisitor $recallVisitor
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor)
    {
    }

    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): EPolicyDownloadResponse
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor)
    {
    }


    public function UploadFile(UploadFileVisitor $uploadFileVisitor): UploadFileResponse
    {
        return new UploadFileResponse(200);
    }

    /**
     * 非车产品列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     * @version 4.0
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $response = new NonCarProductListResponse(200);
        $response->printerInit();
        return $response;
    }

    /**
     * 非车产品列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductDetailResponse
     * @version 4.0
     */
    public function nonCarProductDetail(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductDetailResponse
    {
        $request = new NonCarProductDetailRequest();
        $request->setVisitor($nonCarProductListVisitor);

        /*** @var $response NonCarProductDetailResponse */
        $response = $this->execute($request);

        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {
    }

    /**
     * 验车码接口
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $request = new ValidateCarRequest();
        $request->setConfig($validateCarVisitor->argc);
        /*** @var $response ValidateCarResponse */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
